define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'staff/record/index',
                    add_url: 'staff/record/add',
                    edit_url: 'staff/record/edit',
                    del_url: 'staff/record/del',
                    multi_url: 'staff/record/multi',
//                    dragsort_url: 'ajax/weigh',
                    table: 'staffrecord',
                }
            });

            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                escape: false,
                pk: 'id',
                sortName: 'id',
                pagination: true,
                commonSearch: true,
                search: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),sortable: true},
                        {field: 'staff_name', title: __('StaffName')},
                        {field: 'staff_uid', title: __('StaffUid')},
                        {field: 'boss_uid', title: __('BossUid')},
                        // {field: 'image', title: __('Image'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'price', title: __('Price'),sortable: true},
                        {field: 'hours', title: __('Hours'),sortable: true},
                        // {field: 'content', title: __('Content')},
                        {field: 'dt', title: __('Dt'),sortable: true},
                        {field: 'add_time', title: __('Addtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},

                        {field: 'status',title: __('Status'),formatter:function(value){
                                if (value == 1) {
                                    return __('Status1');
                                } else if (value == 2) {
                                    return __('Status2');
                                }
                            },operate:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);

            // 为表格绑定事件
            Table.api.bindevent(table);

            // $(document.body).on("click",".btn-change-recommend",function(e){
            //     var button = $(this);
            //     // console.log(button);
            //     var id = button.data('id');
            //     var params = button.data('params');
            //     var data = {'id':id,'params':params};
            //     Fast.api.ajax({
            //         url: 'chengyu/chengyu/recommend',
            //         data: data
            //     }, function (data) { //success
            //         console.log('===========333333333333333333333333======================');
            //         console.log(data.id);
            //         if(data.status == 1){
            //             console.log('====================');
            //             $('#recommend_'+data.id+' i').removeClass('fa-flip-horizontal');
            //             $('#recommend_'+data.id+' i').removeClass('text-gray');
            //             $('#recommend_'+data.id).attr('data-params','recommend=0');
            //
            //         } else {
            //             console.log('+++++++++++++++');
            //             $('#recommend_'+data.id+' i').addClass('fa-flip-horizontal');
            //             $('#recommend_'+data.id+' i').addClass('text-gray');
            //             $('#recommend_'+data.id).attr('data-params','recommend=1');
            //         }
            //
            //         return true;
            //     }, function (data) { //error
            //         return false;
            //     });
            //     console.log(params);
            // });

            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#', '');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    // params.filter = JSON.stringify({type: typeStr});
                    params.type = typeStr;

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });

            //必须默认触发shown.bs.tab事件
            // $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");

        },
        add: function () {
            Controller.api.bindevent();
            setTimeout(function () {
                $("#c-type").trigger("change");
            }, 100);
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function (table) {
                $(document).on("change", "#c-type", function () {
                    $("#c-pid option[data-type='all']").prop("selected", true);
                    $("#c-pid option").removeClass("hide");
                    $("#c-pid option[data-type!='" + $(this).val() + "'][data-type!='all']").addClass("hide");
                    $("#c-pid").data("selectpicker") && $("#c-pid").selectpicker("refresh");
                });
                Form.api.bindevent($("form[role=form]"));
            },

            formatter:{//渲染的方法
                customtoggle: function (value, row, index) {
                    var table = this.table;
                    var options = table ? table.bootstrapTable('getOptions') : {};
                    var pk = options.pk || "id";
                    var color = typeof this.color !== 'undefined' ? this.color : 'success';
                    var yes = typeof this.yes !== 'undefined' ? this.yes : 1;
                    var no = typeof this.no !== 'undefined' ? this.no : 0;
                    var url = typeof this.url !== 'undefined' ? this.url : '';
                    var confirm = '';
                    var disable = false;
                    if (typeof this.confirm !== "undefined") {
                        confirm = typeof this.confirm === "function" ? this.confirm.call(this, value, row, index) : this.confirm;
                    }
                    if (typeof this.disable !== "undefined") {
                        disable = typeof this.disable === "function" ? this.disable.call(this, value, row, index) : this.disable;
                    }
                    return "<a href='javascript:;' id='recommend_"+row[pk]+"' data-toggle='tooltip' title='" + __('Click to toggle') + "' class='btn-change-recommend " + (disable ? 'btn disabled no-padding' : '') + "' data-index='" + index + "' data-id='"
                        + row[pk] + "' " + (url ? "data-url='" + url + "'" : "") + (confirm ? "data-confirm='" + confirm + "'" : "") + " data-params='" + this.field + "=" + (value == yes ? no : yes) + "'><i class='fa fa-toggle-on text-success text-" + color + " " + (value == yes ? '' : 'fa-flip-horizontal text-gray') + " fa-2x'></i></a>";
                }

            }
        }

    };
    return Controller;
});